import rclpy
from rclpy.node import Node
from nav_msgs.msg import Odometry
from sensor_msgs.msg import Imu, LaserScan
from std_msgs.msg import UInt16

def get_odom(timeout=1.0):
    """
    获取小车速度（Odometry）。
    """
    rclpy.init(args=None)
    node = rclpy.create_node('odom_subscriber')
    msg_data = {}

    def callback(msg):
        msg_data['msg'] = msg
        rclpy.shutdown()

    sub = node.create_subscription(Odometry, 'odom_frame', callback, 10)
    rclpy.spin_once(node, timeout_sec=timeout)
    node.destroy_node()
    return msg_data.get('msg', None)

def get_imu(timeout=1.0):
    """
    获取IMU数据。
    """
    rclpy.init(args=None)
    node = rclpy.create_node('imu_subscriber')
    msg_data = {}

    def callback(msg):
        msg_data['msg'] = msg
        rclpy.shutdown()

    sub = node.create_subscription(Imu, 'imu_frame', callback, 10)
    rclpy.spin_once(node, timeout_sec=timeout)
    node.destroy_node()
    return msg_data.get('msg', None)

def get_laser(timeout=1.0):
    """
    获取激光雷达数据。
    """
    rclpy.init(args=None)
    node = rclpy.create_node('laser_subscriber')
    msg_data = {}

    def callback(msg):
        msg_data['msg'] = msg
        rclpy.shutdown()

    sub = node.create_subscription(LaserScan, 'laser_frame', callback, 10)
    rclpy.spin_once(node, timeout_sec=timeout)
    node.destroy_node()
    return msg_data.get('msg', None)

def get_battery(timeout=1.0):
    """
    获取电池电压。
    """
    rclpy.init(args=None)
    node = rclpy.create_node('battery_subscriber')
    msg_data = {}

    def callback(msg):
        msg_data['msg'] = msg
        rclpy.shutdown()

    sub = node.create_subscription(UInt16, 'battery', callback, 10)
    rclpy.spin_once(node, timeout_sec=timeout)
    node.destroy_node()
    return msg_data.get('msg', None)